﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using aaaSoft.SkinEngine.SkinHelpers;
using System.Diagnostics;

namespace aaaSoft.SkinEngine.MyControls
{
    class MyTabControl:IMyControl
    {
        TabControl tcBase;
        SkinEngine skinEng;

        public MyTabControl(TabControl tc, SkinEngine skinEng)
        {
            tcBase = tc;
            this.skinEng = skinEng;
        }

        public void StopControlSkin()
        {
            tcBase.DrawMode = TabDrawMode.Normal;
            tcBase.DrawItem -= new DrawItemEventHandler(tcBase_DrawItem);
        }

        public void StartControlSkin()
        {
            tcBase.DrawMode = TabDrawMode.OwnerDrawFixed;
            tcBase.DrawItem += new DrawItemEventHandler(tcBase_DrawItem);            
        }

        void tcBase_DrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            List<Rectangle> backRects = new List<Rectangle>();

            //上方部分
            if (e.State == DrawItemState.None)
            {
                Rectangle rect = new Rectangle(new Point(0, 0), tcBase.Size);
                rect.Height = e.Bounds.Top;
                backRects.Add(rect);
            }

            //画标签右边部分
            if (e.Index == tcBase.TabCount - 1)
            {
                Rectangle rect = e.Bounds;
                rect.X += e.Bounds.Width;
                rect.Width = tcBase.Width - e.Bounds.Right;
                backRects.Add(rect);
            }

            //画背景矩形
            foreach (Rectangle rect in backRects)
            {
                g.FillRectangle(new SolidBrush(skinEng.BackColor), rect);
            }

            //画按钮状
            {
                Rectangle rect = e.Bounds;
                if (e.State == DrawItemState.None)
                {
                    rect.Height += 5;
                }
                float ColorDeep = 0.8F;
                if (e.State == DrawItemState.Selected)
                {
                    ColorDeep = 1.1F;
                }
                GraphicHelper.DrawRectButton(g, rect, skinEng.BackColor, ColorDeep);
            }

            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.FormatFlags = StringFormatFlags.NoWrap;

            string displayText = SkinHelpers.GraphicHelper.GetAppropriateString(g, tcBase.TabPages[e.Index].Text, tcBase.Font, e.Bounds);
            g.DrawString(displayText, tcBase.Font, Brushes.Black, e.Bounds, sf);
        }

        void tcBase_Selected(object sender, TabControlEventArgs e)
        {
            
        }

        void tcBase_Deselected(object sender, TabControlEventArgs e)
        {
            
        }


    }
}
